/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.KhachHang;
import util.HibernateUtil;

public class KhachHangDAO {

    public static List<KhachHang> danhSachKhachHang() {
        List<KhachHang> ds = new ArrayList<KhachHang>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from KhachHang";
        Query query = session.createQuery(hql);
        ds = query.list();
        return ds;
    }

    public static boolean ktraTaiKhoan(String tenDangNhap) {
        KhachHang kh = KhachHangDAO.layKhachHang(tenDangNhap);
        if (kh != null) {
            return true;
        }
        return false;
    }

    public static KhachHang layKhachHang(String tenDangNhap) {
        KhachHang khachHang = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "select kh from KhachHang kh where kh.tenDangNhap= :tenDangNhap";
        Query query = session.createQuery(hql);
        query.setString("tenDangNhap", tenDangNhap);
        khachHang = (KhachHang) query.uniqueResult();
        session.close();
        return khachHang;
    }

    public static KhachHang layKhachHang(int maKhachHang) {
        KhachHang khachHang = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "select kh from KhachHang kh where kh.maKhachHang= :maKhachHang";
        Query query = session.createQuery(hql);
        query.setInteger("maKhachHang", maKhachHang);
        khachHang = (KhachHang) query.list();
        session.close();
        return khachHang;
    }

    public static boolean themKhachHang(KhachHang khachHang) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        session.save(khachHang);
        transaction.commit();
        session.close();
        return true;
    }

    public static boolean ktraSoDienThoai(String soDienThoai) {
        List<KhachHang> ds = new ArrayList<KhachHang>();
        ds = danhSachKhachHang();
        for (KhachHang khachHang : ds) {
            if (khachHang.getSoDienThoai().equals(soDienThoai)) {
                return true;
            }
        }
        return false;
    }

    public static boolean ktraEmail(String email) {
        List<KhachHang> ds = danhSachKhachHang();
        for (KhachHang khachHang : ds) {
            if (khachHang.getEmail().equals(email)) {
                return true;
            }
        }
        return false;
    }

    public static int layMaKhachHangMax() {
        int maKH = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "select max(dh.maKhachHang) from KhachHang dh";
        Query query = session.createQuery(hql);
        Object obj=query.uniqueResult();
        maKH=(Integer)obj;
        return maKH;
    }
}
